import {Injectable} from "@angular/core";
import {PageService} from "@core/service/page/PageService";
import {DiskService, SearchReq} from "@core/service/DiskService";
import {NzMessageService} from "ng-zorro-antd/message";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {UserPublicInfo} from "@shared/models/UserInfo";
import {Params, Router} from "@angular/router";
import {UserActionService} from "@core/service/UserActionService";
import {NzContextMenuService} from "ng-zorro-antd/dropdown";
import {NzModalService} from "ng-zorro-antd/modal";
import {FileUploadService} from "@core/service/FileUploadService";
import {NzImageService} from "ng-zorro-antd/image";
import {ClipboardService} from "@core/service/ClipboardService";
import {FileInfo} from "@shared/models/FileInfo";
export class UserListData{
  list:UserPublicInfo[]=[]
  loading=true
  userTotal: number=-1
  listPage=1;
}
export class SearchFileData{
  list:FileInfo[]=[]
  loading = true
  total: number=500
  listPage=1;
}
@Injectable({
  providedIn: 'root',
})
export class SharePageService extends PageService{

  searchFileData=new SearchFileData()
  userListPageData:UserListData = new UserListData()
  searchModel: 'user'|'file'='user';
  searchValue = '';
  pageName:'user'|'file'|'disk'|'main'='user'
  constructor(
    protected diskService: DiskService,
    protected userActionService:UserActionService,
    protected message: NzMessageService,
    protected router: Router,
    protected nzContextMenuService: NzContextMenuService,
    protected dialogService: NzModalService,
    protected fileUploadService: FileUploadService,
    protected nzImageService: NzImageService,
    protected clipboardService:ClipboardService
) {
  super(
    diskService,userActionService,message,router,
    nzContextMenuService,dialogService,fileUploadService,
    nzImageService,clipboardService);
  }
  load(path: string,params:Params){
    super.load(path,params)
    this.pageName="disk"
  }

  loadUserRoots(page:number=1) {
    this.pageName='main'
    console.log('获取所有用户：',page)
    BBYUtils.handelResult(
      this.diskService.userRoots(page),
      res => {
        this.userListPageData.list=res.data.list
        if (page==1) {
          this.userListPageData.userTotal=res.data.total
        }
        this.userListPageData.listPage=page
        this.userListPageData.loading=false
      },
      res => {
        this.userListPageData.loading=false
        this.message.error(res.msg)
      }
    )
  }
  searchUser(params: Params) {
    if (params.search){
      let page = params.page||1;
      this.pageName='user'
      this.searchModel='user'
      if (params.search!=this.searchValue){
        this.searchValue=params.search
      }
      console.log('搜索用户：',params)
      BBYUtils.handelResult(
        this.userActionService.searchUser(params.search,page),
        res => {
          if (page==1) {
            this.userListPageData.userTotal=res.data.total
          }
          this.userListPageData.listPage=page
          this.userListPageData.list=res.data.list
          this.userListPageData.loading=false
        },
        res => {
          this.userListPageData.loading=false
          this.message.error(res.msg)
        }
      )
    }else {
      this.router.navigate(['/share'])
    }
  }
  searchFileGlobal(params: Params) {
    if (params.search&&params.search!=this.searchValue){
      this.searchValue=params.search
      this.searchModel='file'
    }
    this.pageName='file'
    this.searchFileData.loading = true;
    let data:SearchReq={
      target:params.id||'',
      keyword:params.search,
      page:params.page||1,
      limit:params.limit||12
    }
    BBYUtils.handelResult(
      this.diskService.search(data),
      res => {
        super.dealFileList(res.data.list)
        this.searchFileData.list=res.data.list
        this.searchFileData.loading=false
        if (res.data.end){
          let total=(data.page-1)*data.limit
          total = (total<0? 0:total) + res.data.total
          console.log('total',total)
          this.searchFileData.total=total
          this.searchFileData.listPage=data.page
        }else this.searchFileData.total=500
      },
      res => {
        this.message.error(res.msg)
      }
    )
    console.log('全局搜索：',params)
  }
  /**
   * 处理请求入口
   * @param url 不带参数
   * @param params
   */
  dealUrl(url: string,params:Params) {
    super.dealUrl(url,params)
    let path = this.openUrl.substring(7)
    if (path.startsWith("disk")){
      path = path.substring(5)
      if (path||params.id){
        this.load(path,params)
      }else {
        this.router.navigate(['/share'])
      }
    }else if (path.startsWith('file')){
      this.searchFileGlobal(params)
    }else if (path.startsWith('user')){
      this.searchUser(params)
    }else {
      this.loadUserRoots(params.page)
    }
  }


}
